VERSION 5.00
Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX"
Begin VB.Form frmMain 
   Caption         =   "Form1"
   ClientHeight    =   7005
   ClientLeft      =   120
   ClientTop       =   450
   ClientWidth     =   6855
   LinkTopic       =   "Form1"
   ScaleHeight     =   7005
   ScaleWidth      =   6855
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdClose 
      Caption         =   "Close"
      Height          =   375
      Left            =   5640
      TabIndex        =   7
      Top             =   6360
      Width           =   975
   End
   Begin VB.CommandButton cmdSend 
      Caption         =   "Send"
      Height          =   375
      Left            =   5640
      TabIndex        =   6
      Top             =   5760
      Width           =   975
   End
   Begin VB.TextBox txtOut 
      Height          =   975
      Left            =   120
      TabIndex        =   5
      Top             =   5760
      Width           =   5295
   End
   Begin VB.TextBox txtIn 
      Height          =   3255
      Left            =   120
      MultiLine       =   -1  'True
      ScrollBars      =   2  'Vertical
      TabIndex        =   4
      Top             =   2040
      Width           =   6615
   End
   Begin VB.TextBox txtName 
      Height          =   375
      Left            =   2040
      TabIndex        =   3
      Top             =   1440
      Width           =   2175
   End
   Begin VB.TextBox txtIP 
      Height          =   375
      Left            =   120
      TabIndex        =   2
      Top             =   1440
      Width           =   1695
   End
   Begin VB.CommandButton cmdConnect 
      Caption         =   "Connect"
      Height          =   375
      Left            =   4560
      TabIndex        =   1
      Top             =   1440
      Width           =   975
   End
   Begin VB.CommandButton cmdListen 
      Caption         =   "Listen"
      Height          =   375
      Left            =   5760
      TabIndex        =   0
      Top             =   1440
      Width           =   975
   End
   Begin MSWinsockLib.Winsock wsChat 
      Left            =   6240
      Top             =   840
      _ExtentX        =   741
      _ExtentY        =   741
      _Version        =   393216
      Protocol        =   1
   End
   Begin VB.Label Label2 
      Caption         =   "chat ID"
      Height          =   375
      Left            =   2040
      TabIndex        =   9
      Top             =   960
      Width           =   2055
   End
   Begin VB.Label Label1 
      Caption         =   "I.P. Address"
      Height          =   375
      Left            =   120
      TabIndex        =   8
      Top             =   960
      Width           =   1455
   End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public index As Integer

Private Sub cmdClose_Click()
wsChat.Close
cmdClose.Enabled = False
cmdSend.Enabled = False
txtName.Enabled = True
cmdListen.Enabled = True
cmdConnect.Enabled = True
txtIn.text = "-----Connection Closed-----" & vbCrLf
End Sub

Private Sub cmdConnect_Click()
If txtIP.text = "" Or txtName.text = "" Then
MsgBox "Please enter both IP and Alias Name", vbCritical, "Error"
txtName.SetFocus
Exit Sub
End If
On Error Resume Next
wsChat.Close
wsChat.Connect txtIP.text, 1234
cmdClose.Enabled = True
cmdListen.Enabled = False
cmdConnect.Enabled = False
txtName.Enabled = False
End Sub


Private Sub AddText(ByVal text As String, ByRef Box As TextBox)
Box.text = Box.text & text & vbCrLf
Box.SelStart = Len(Box.text)
End Sub


Private Sub cmdListen_Click()
If txtName.text = "" Then
MsgBox "You must enter an alias first!", vbCritical, "Error!"
txtName.SetFocus
Exit Sub
End If
txtIP.text = wsChat.LocalIP
wsChat.Close
wsChat.LocalPort = 1234
wsChat.Listen
cmdClose.Enabled = True
cmdListen.Enabled = False
cmdConnect.Enabled = False
txtName.Enabled = False
AddText "-----Waiting for connection-----", txtIn
End Sub



Private Sub cmdSend_Click()
wsChat.SendData "[" & txtName.text & "]" & txtOut.text
AddText "[" & txtName.text & "]" & txtOut.text, txtIn
txtOut.text = ""
txtOut.SetFocus
End Sub


Private Sub Form_Load()
index = 0

End Sub

Private Sub wsChat_Connect()
Do
DoEvents
Loop Until wsChat.State = sckConnected Or wsChat.State = sckError
If wsChat.State = sckConnected Then
AddText "-----Connection Established-----" & vbCrLf, txtIn
cmdSend.Enabled = True
txtName.Enabled = False
txtOut.SetFocus
Else
AddText "-----Connection Failed-----" & vbCrLf, txtIn
End If
End Sub



Private Sub wsChat_ConnectionRequest(ByVal requestID As Long)
wsChat.Close
wsChat.Accept requestID
AddText "-----Connection Established----" & vbCrLf, txtIn
cmdSend.Enabled = True
txtName.Enabled = False
txtOut.SetFocus
End Sub



Private Sub wsChat_DataArrival(ByVal bytesTotal As Long)
Dim incoming As String
wsChat.GetData incoming
AddText incoming, txtIn
End Sub



Private Sub wsChat_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
If Number <> 0 Then
AddText "-----Error[" & Description & "]-----" & vbCrLf, txtIn
Call cmdClose_Click
End If
End Sub

